package im.elephas.androfan;

import im.elephas.fanfouapi.FanfouApi;
import im.elephas.fanfouapi.events.IChangeEvent;
import im.elephas.fanfouapi.events.IErrorEvent;
import im.elephas.fanfouapi.events.IStatusesEvent;
import im.elephas.fanfouapi.items.StatusItem;
import im.elephas.util.DateFormater;
import im.elephas.util.ImageGetter;
import im.elephas.util.TagFilter;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalInfoActivity extends Activity implements IStatusesEvent,
		IErrorEvent, OnClickListener {

	private String name;
	private String statuses;
	private String friends;
	private String followers;
	private String profile;
	private String description;
	private String id;

	private Button button;
	private TextView countTextView;
	private TextView followersTextView;
	private TextView friendsTextView;
	private ImageView imageView;
	private TextView nameTextView;
	private LinearLayout statusesLinearLayout;
	private ProgressBar progressBar;

	private FanfouApi mFanfouApi;

	private boolean isFriend;

	private PersonalInfoActivity mPersonalInfoActivity = this;
	private List<StatusItem> listStatusesItems;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		name = getIntent().getStringExtra("name");
		statuses = getIntent().getStringExtra("statuses");
		friends = getIntent().getStringExtra("friends");
		followers = getIntent().getStringExtra("followers");
		profile = getIntent().getStringExtra("profile");
		description = getIntent().getStringExtra("description");
		id = getIntent().getStringExtra("id");

		setTitle("@" + name + "的详细资料");

		mFanfouApi = FanfouApi.getInstance();

		refreshUi();
		mFanfouApi.getUserTimeline(10, 1, id, this, this);
	}

	private void refreshUi() {
		setContentView(R.layout.personalinfo);
		button = (Button) findViewById(R.id.personalButton);
		countTextView = (TextView) findViewById(R.id.personalCountTextView);
		followersTextView = (TextView) findViewById(R.id.personalFollowersTextView);
		friendsTextView = (TextView) findViewById(R.id.personalFriendsTextView);
		imageView = (ImageView) findViewById(R.id.personalImageView);
		nameTextView = (TextView) findViewById(R.id.personalNameTextView);
		statusesLinearLayout = (LinearLayout) findViewById(R.id.personalStatusesLinearLayout);
		progressBar = (ProgressBar) findViewById(R.id.personalProgressBar);

		countTextView.setText(statuses + "\n消息");
		followersTextView.setText(followers + "\n被关注");
		friendsTextView.setText(friends + "\n关注的");
		nameTextView.setText(name);
		imageView.setImageBitmap(ImageGetter.getBitMap(profile));

		List<String> lu = mFanfouApi.getFirendsListIds();
		if (lu.contains(id)) {
			isFriend = true;
			button.setText("撤销好友");
		} else {
			isFriend = false;
			button.setText("加为好友");
		}

		button.setOnClickListener(this);
	}

	@Override
	public void errorHandler() {
		mPersonalInfoActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Builder dialog = new AlertDialog.Builder(mPersonalInfoActivity);
				dialog.setTitle("请检查网络设置!");
				dialog.show();
				finish();
			}
		});
	}

	@Override
	public void statusesHandler(final List<StatusItem> lsi) {
		listStatusesItems = lsi;
		mPersonalInfoActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				refreshStatuses();
			}
		});
	}

	private void refreshStatuses() {
		progressBar.setVisibility(View.GONE);

		boolean colorDark = false;
		for (final StatusItem item : listStatusesItems) {
			View view = getLayoutInflater().inflate(
					R.layout.statusessimpleitem, null);
			if (colorDark)
				view.setBackgroundColor(Color.argb(255, 202, 222, 235));
			else
				view.setBackgroundColor(Color.argb(255, 237, 237, 237));
			colorDark = !colorDark;
			TextView statusTextView = (TextView) view
					.findViewById(R.id.statusTextView);
			TextView sourceTextView = (TextView) view
					.findViewById(R.id.sourceTextView);
			TextView timeTextView = (TextView) view
					.findViewById(R.id.timeTextView);
			nameTextView.setText(item.getUser().getName());
			statusTextView.setText(item.getText());
			sourceTextView.setText("通过: " + TagFilter.getTag(item.getSource()));
			timeTextView.setText(DateFormater.formatDate(item.getCreatedAt()));
			statusesLinearLayout.addView(view);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		refreshStatuses();
	}

	@Override
	public void onClick(View v) {
		button.setEnabled(false);
		if (isFriend) {
			mFanfouApi.destroyFriendship(id, new IChangeEvent() {

				@Override
				public void changeHandler(boolean success) {
					mPersonalInfoActivity.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Toast.makeText(mPersonalInfoActivity, "撤销成功",
									Toast.LENGTH_SHORT).show();
							isFriend = false;
							button.setText("加为好友");
							button.setEnabled(true);
						}
					});
				}
			}, this);
		} else {
			mFanfouApi.createFriendship(id, new IChangeEvent() {

				@Override
				public void changeHandler(boolean success) {
					mPersonalInfoActivity.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Toast.makeText(mPersonalInfoActivity, "加入成功",
									Toast.LENGTH_SHORT).show();
							isFriend = false;
							button.setText("撤销好友");
							button.setEnabled(true);
						}
					});
				}
			}, this);
		}
	}

}